import router from './index'

const isIncludeAllChildren = (value, target) => {
  const res = target.map(item => value.includes(item))
  return !res.includes(false)
}

/** 
 * 是否有按钮级别的权限
 * value: string | array<string>
 * 
 * @return boolean
 */
export const hasAuth = (value) => {
  if(!value) return false
  /** 从当前路由的`meta`字段里获取按钮级别的所有自定义`code`值 */
  const metaAuths = router.currentRoute.meta.auths
  if (!metaAuths) return false
  if(typeof value === 'string'){
    return metaAuths.includes(value)
  } else {
    return isIncludeAllChildren(metaAuths, value)
  }
}